
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:ayj_flutter_chat_app/common/utils/jh_permission_utils.dart';
import 'package:ayj_flutter_chat_app/pages/chat/video/video_controller.dart';
import 'package:ayj_flutter_chat_app/pages/chat/video/widget/video_widget.dart';
import 'package:flutter/material.dart';
import 'package:ayj_flutter_chat_app/common/configs/project_config.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../common/third/agora/engine/agora_engine_controller.dart';
import '../voice/widget/voice_back_widget.dart';
import '../voice/widget/voice_buttons_widget.dart';
import '../voice/widget/voice_header_widget.dart';
import '../voice/widget/voice_shrink_widget.dart';
import '../voice/widget/voice_time_widget.dart';

class VideoPage extends GetWidget<VideoController> {
  static const String sName = '/video_page';

  static Future<void> open(BuildContext context, {
    dynamic arguments,
  }) async {

    bool status = await JhPermissionUtils.checkPermissions([Permission.microphone, Permission.camera,Permission.videos,Permission.location]);
    if (status) {
      Get.put(AgoraEngineController());
      return JhNavUtils.toNamed( VideoPage.sName,arguments: arguments);
    } else {
      JhPermissionUtils.JhDialogOpenAppSettings(context);
      return;
    }
  }

  final Map? arguments;
  const VideoPage({Key? key, this.arguments});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset:false,
        appBar: _buildAppBar(),
        body: _buildBody(context)
    );
  }

  PreferredSizeWidget? _buildAppBar(){
    return null;
  }

  Widget _buildBody(context){
    AgoraEngineController engineController = AgoraEngineController.to;
    double margin = engineController.buttonsW * .6;
    double bottomSize = engineController.buttonsW * 1.5;
    String url = 'https://randomuser.me/api/portraits/women/17.jpg';

    LogUtils.e('remoteUid  _buildBody== ${engineController.remoteUid}');
    return Container(
        color: Colours.colorFDFFFE,
        child: Stack(
          children: [
            Obx(()=>engineController.openCamera.value?_buildVideoWidget():VoiceBackWidget(url: url)),

            Positioned(
              left: margin,
              right: margin,
              bottom: bottomSize,
              height: engineController.buttonsW,
              child: Obx(()=> VoiceButtonsWidget(
                buttonW: engineController.buttonsW,
                type: engineController.voiceMeetingType.value,
                cType: ControllerType.meetingVideo,
                receiveOnTap: (){
                  engineController.receiveOnTap('1',1);
                  // engineController.engine?.startPreview();
                },
                cancelOnTap: (){
                  engineController.cancelOnTap();
                },
                speakOnTap: engineController.speakOnTap,
                voiceOnTap: engineController.voiceOnTap,
                cameraOnTap: engineController.cameraOnTap,
                changeCameraOnTap: engineController.changeCameraOnTap,
              )),),
            Positioned(
              left: 0,
              right: 0,
              bottom: bottomSize * 2 + 30.h,
              child: Center(
                child: Obx(()=> VoiceTimeWidget(type: engineController.voiceMeetingType.value,
                  cType: ControllerType.meetingVideo,
                ),
                ),),
            ),
            Positioned(
                top: 32.h+JhScreenUtils.topSafeHeight,
                left: 32.w,
                child: VoiceShrinkWidget(cType: ControllerType.meetingVideo,onTap: (v){
                  controller.close.value = true;
                },)),
            Obx(()=> (engineController.voiceMeetingType.value==MeetingStatusType.received||engineController.voiceMeetingType.value==MeetingStatusType.call)?
            Positioned(
                top: 65.h,
                right: 15.w,
                width: 90.w,
                height: 160.h,
                child: Obx(()=> engineController.remoteUid.isEmpty?VideoWidget(
                  url: url,
                  name: '贾乃亮',
                ):_buildMiniVideoWidget(context))) :
            Positioned(
                top: JhScreenUtils.screenHeight * 0.3,
                left: 0,
                right: 0,
                child: Center(
                  child: VoiceHeaderWidget(
                    url: url,
                    name: '贾乃亮',
                    size: engineController.buttonsW*1.5,
                  ),
                ))
            ),
          ],
        )
    );
  }

  Widget _buildVideoWidget(){
    AgoraEngineController engineController = AgoraEngineController.to;
    return AgoraVideoView(
      controller: VideoViewController(
        rtcEngine: engineController.getRtcEngine(),
        canvas: VideoCanvas(uid: 0),
      ),
      onAgoraVideoViewCreated: (viewId) {
        engineController.engine?.startPreview();
      },
    );
  }

  Widget _buildMiniVideoWidget(context){
    return Obx(()=>controller.close.value?Container():AgoraEngineController.to.buildAgoraVideoView(context));
    //
    return AgoraEngineController.to.buildAgoraVideoView(context);
    AgoraEngineController engineController = AgoraEngineController.to;
    return AgoraVideoView(
      controller: VideoViewController.remote(
        rtcEngine: engineController.getRtcEngine(),
        canvas: VideoCanvas(uid: engineController.remoteUid.first),
        connection: RtcConnection(channelId: engineController.channelID),
      ),
    );
  }

}
